package com.fyd.controller;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyd.entity.Ccc;
import com.fyd.entity.DbConf;
import com.fyd.mapper.CccMapper;
import com.fyd.mapper.DbConfMapper;
import com.fyd.service.DynamicChangeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private DbConfMapper dbConfMapper;
    @Resource
    private CccMapper cccMapper;
    @Resource
    private DynamicChangeService dynamicChangeService;

    @RequestMapping("/tet")
    public String test(){
        QueryWrapper<DbConf> queryWrapper = new QueryWrapper();
        List<DbConf> dbConfs = dbConfMapper.selectList(queryWrapper);
        System.out.println(dbConfs);
        System.out.println(dynamicChangeService.addDatasource(dbConfs.get(0)));
        //切换
        DynamicDataSourceContextHolder.push("db2");
        QueryWrapper<Ccc> queryWrapper1= new QueryWrapper();
        List<Ccc> cccs = cccMapper.selectList(queryWrapper1);
        System.out.println(cccs);
        DynamicDataSourceContextHolder.poll();
        List<DbConf> dbaaConfs = dbConfMapper.selectList(queryWrapper);
        System.out.println(dbaaConfs);
        List<Ccc> sss = cccMapper.selectList(queryWrapper1);
        System.out.println(sss);
        return "aaa";
    }
}
